package cn.bdqn.service.impl;

import cn.bdqn.mapper.SalChanceMapper;
import cn.bdqn.pojo.Customer;
import cn.bdqn.pojo.SalChance;
import cn.bdqn.pojo.User;
import cn.bdqn.service.CustomerService;
import cn.bdqn.service.SalChanceService;
import cn.bdqn.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SalChanceServiceImpl implements SalChanceService {
    @Resource
    SalChanceMapper salChanceMapper;
    @Resource
    CustomerService customerService;
    @Resource
    UserService userService;

    @Override
    public IPage<SalChance> getChancesByPage(String name, String title, Integer pageNo, Integer pageSize,Long createId) {
        //组装分页条件
        Page<SalChance> page=new Page<>(pageNo,pageSize);
        //组装过滤条件 动态sql
        QueryWrapper<SalChance> qw=new QueryWrapper();
        qw.like(name!=null&&!"".equals(name),"chc_cust_name",name);
        qw.like(title!=null&&!"".equals(title),"chc_title",title);
        qw.eq("chc_create_id",createId);
        qw.eq("chc_status",'1');

        //调用分页方法
        IPage<SalChance> salChanceIPage = salChanceMapper.selectPage(page, qw);
        return salChanceIPage;
    }

    @Override
    public void addChance(SalChance chance) {
        if (chance.getChcDueId()==null){
            salChanceMapper.insert(chance);
        }else{
         User user = userService.selectOne(chance.getChcDueId());
        chance.setChcDueTo(user.getUsrName());
            salChanceMapper.insert(chance);
        }

    }

    @Override
    public SalChance getChance(Long id) {
        return salChanceMapper.selectById(id);
    }

    @Override
    public void updateChance(SalChance chance) {
        User user = userService.selectOne(chance.getChcDueId());
        chance.setChcDueTo(user.getUsrName());
        salChanceMapper.updateById(chance);
    }

    @Override
    public void deleteChance(Long id) {
        salChanceMapper.deleteById(id);
    }

    @Override
    public IPage<SalChance> getChancesByPage(String name, String linkMan, String title, Integer pageNo, Integer pageSize,Long createId) {
        //组装分页条件
        Page<SalChance> page=new Page<>(pageNo,pageSize);
        //组装查询条件
        QueryWrapper<SalChance> qw=new QueryWrapper<>();
        qw.in("chc_status","2","3","4");
        qw.like(name!=null&&!"".equals(name),"chc_cust_name",name);
        qw.like(linkMan!=null&&!"".equals(linkMan),"chc_linkman",linkMan);
        qw.like(title!=null&&!"".equals(title),"chc_title",title);
        User user = userService.selectOne(createId);
        Long usrRoleId = user.getUsrRoleId();
        if (usrRoleId!=2){
            qw.eq("chc_create_id",createId);
        }
        if (usrRoleId==2){
            qw.eq(createId!=null,"chc_due_id",createId);
        }

        //调用分页方法
        IPage<SalChance> chancePage=salChanceMapper.selectPage(page, qw);
        //返回分页对象
        return chancePage;

    }

    @Override
    public void successChance(Long id) {
        SalChance salChance = salChanceMapper.selectById(id);
        salChance.setChcStatus("3");
        this.updateChance(salChance);

        //向客户信息表添加一调数据
        //客户编号
        Customer customer=new Customer();

        int rand= (int)(Math.random()*(1000-100)+100);
        SimpleDateFormat df=new SimpleDateFormat("yyMMddHHmmss");
        customer.setCustNo("KH"+df.format(new Date())+rand);


        customer.setCustName(salChance.getChcCustName());
        customer.setCustRegion(salChance.getChcCustRegion());
        customer.setCustManagerId(salChance.getChcDueId());
        customer.setCustManagerName(salChance.getChcDueTo());
        customer.setCustLinkman(salChance.getChcLinkman());
        customer.setCustLevel(Integer.valueOf(salChance.getChcCustLevel()));
        customer.setCustSatisfy(5);
        customer.setCustCredit(5);
        customer.setCustTel(salChance.getChcTel());
        customer.setCustStatus("1");
        //新增客户
        customerService.addCustomer(customer);
    }

    @Override
    public void failChance(Long id) {
        SalChance salChance = salChanceMapper.selectById(id);
        salChance.setChcStatus("4");
        this.updateChance(salChance);
    }


}
